package com.yq.core.util.crawler;

import com.google.common.collect.Maps;
import com.yq.core.util.JDBC;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class Magiccards {

    public static String urlp = "https://magiccards.info";
    public static String urlv = "";
    public static JDBC ew = new JDBC();
    public static Map<String, String> objs = new HashMap<String, String>(){{
        put("/xln/cn.html", "1");
        put("/hou/cn.html", "1");
        put("/akh/cn.html", "1");
        put("/aer/cn.html", "1");
        put("/kld/cn.html", "1");
        put("/emn/cn.html", "1");
        put("/soi/cn.html", "1");
        put("/ogw/cn.html", "1");
        put("/bfz/cn.html", "1");
        put("/dtk/cn.html", "1");
        put("/frf/cn.html", "1");
        put("/ktk/cn.html", "1");
        put("/jou/cn.html", "1");
        put("/bng/cn.html", "1");
        put("/ths/cn.html", "1");
        put("/dgm/cn.html", "1");
        put("/gtc/cn.html", "1");
        put("/rtr/cn.html", "1");
        put("/avr/cn.html", "1");
        put("/dka/cn.html", "1");
        put("/isd/cn.html", "1");
        put("/nph/cn.html", "1");
        put("/mbs/cn.html", "1");
        put("/som/cn.html", "1");
        put("/roe/cn.html", "1");
        put("/wwk/cn.html", "1");
        put("/zen/cn.html", "1");
        put("/arb/cn.html", "1");
        put("/cfx/cn.html", "1");
        put("/ala/cn.html", "1");
        put("/eve/cn.html", "1");
        put("/shm/cn.html", "1");
        put("/mt/cn.html", "1");
        put("/lw/cn.html", "1");
        put("/fut/cn.html", "1");
        put("/pc/cn.html", "1");
        put("/ts/cn.html", "1");
        put("/tsts/cn.html", "1");
        put("/cs/cn.html", "1");
        put("/di/cn.html", "1");
        put("/gp/cn.html", "1");
        put("/rav/cn.html", "1");
        put("/sok/cn.html", "1");
        put("/bok/cn.html", "1");
        put("/chk/cn.html", "1");
        put("/5dn/cn.html", "1");
        put("/ds/cn.html", "1");
        put("/ori/cn.html", "1");
        put("/m15/cn.html", "1");
        put("/m14/cn.html", "1");
        put("/m13/cn.html", "1");
        put("/m12/cn.html", "1");
        put("/m11/cn.html", "1");
        put("/m10/cn.html", "1");
        put("/10e/cn.html", "1");
        put("/9e/cn.html", "1");
        put("/cn2/cn.html", "1");
        put("/cns/cn.html", "1");
        put("/ima/cn.html", "1");
        put("/mm3/cn.html", "1");
        put("/ema/cn.html", "1");
        put("/mm2/cn.html", "1");
        put("/c16/cn.html", "1");
        put("/c15/cn.html", "1");
        put("/c14/cn.html", "1");
        put("/9eb/cn.html", "1");
        put("/w17/cn.html", "1");
        put("/w16/cn.html", "1");
        put("/p3k/cn.html", "1");
        put("/ugin/cn.html", "1");
    }};

    public static void main(String[] args) throws Exception {

        // 初始化证书
        javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
        javax.net.ssl.TrustManager tm = new miTM();
        trustAllCerts[0] = tm;
        javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, null);
        javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

        // 获取第一层列表
        //getHtml("http://www.stats.gov.cn/tjsj/tjbz/xzqhdm/201703/t20170310_1471429.html");
        getMagicList("http://localhost:8300/assets/magic.html");

    }

    public static void getMagicList(String url) {
        try {
            Document doc = Jsoup.connect(url).get();
            Elements elements = doc.select("a");
            for(int i=0; i<elements.size(); i++) {
                urlv = "";
                Element element = elements.get(i);
                String msoNormal = element.toString();


                System.out.println(element.attr("href"));
                if(objs.get(element.attr("href"))==null) {
                    urlv = urlp + element.attr("href");
                    getExpansions(urlp + element.attr("href"));
                }

                System.out.println("put(\"" + element.attr("href") + "\", \"1\");");
            }

            System.out.println("END");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void getExpansions(String url) throws Exception {
        Document doc = Jsoup.connect(url).get();
        Element table = doc.select("table").get(3);

        setEntity(table.getElementsByClass("even"));
        setEntity(table.getElementsByClass("odd"));
    }

    public static void setEntity(Elements evens) throws Exception {
        for(int i=0; i<evens.size(); i++) {
            Element element = evens.get(i);
            Elements tds = element.getElementsByTag("td");

            Map<String, String> entity = Maps.newHashMap();
            entity.put("uid", UUID.randomUUID().toString().replaceAll("-", ""));
            entity.put("version_url", urlv);


            for(int j=0; j<tds.size(); j++) {
                Element td = tds.get(j);
                if(j==0) {
                    // 序号
                    entity.put("card_no", td.text());
                }
                if(j==1) {
                    // 地址、名称
                    entity.put("card_url", urlp + td.getElementsByTag("a").attr("href"));
                    entity.put("card_name", td.text().replaceAll("'", "’"));
                }
                if(j==2) {
                    // 类别
                    entity.put("card_type", td.text());
                }
                if(j==3) {
                    // 法力值
                    entity.put("consume", td.text());
                }
                if(j==4) {
                    // 稀有度
                    entity.put("rare", td.text());
                }
                if(j==5) {
                    // 画家
                    entity.put("painter", td.text().replaceAll("'", "`"));
                }
                if(j==6) {
                    // 版本
                    entity.put("version", td.text().replaceAll("'", "’"));
                }
            }



            String sql = "insert into magic";
            StringBuffer keys = new StringBuffer();
            StringBuffer values = new StringBuffer();

            for (Map.Entry<String, String> entry : entity.entrySet()) {
                keys.append(entry.getKey() + ",");
                values.append("'" + entry.getValue() + "'" + ",");
            }

            String key = keys.toString();
            String value = values.toString();

            sql = sql + "("+key.substring(0, key.length()-1)+") values (" + value.substring(0, value.length()-1) + ")";

            //System.out.println(sql);
            ew.insertSQL(sql);
            //System.out.println("=================================================");
        }
    }

    public static void getInfo(String url) throws Exception {
        Document doc = Jsoup.connect(url).get();
        //String msoNormal = element.toString();

        Elements evens = doc.select("table").get(3).getElementsByClass("even");
        for(int i=0; i<evens.size(); i++) {
            Element element = evens.get(i);
            System.out.println(element.getElementsByTag("td"));

            //System.out.println(element.text());
            System.out.println("=================================================");
        }

    }

    // 模拟证书
    static class miTM implements javax.net.ssl.TrustManager,
            javax.net.ssl.X509TrustManager {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        public boolean isServerTrusted(
                java.security.cert.X509Certificate[] certs) {
            return true;
        }

        public boolean isClientTrusted(
                java.security.cert.X509Certificate[] certs) {
            return true;
        }

        public void checkServerTrusted(
                java.security.cert.X509Certificate[] certs, String authType)
                throws java.security.cert.CertificateException {
            return;
        }

        public void checkClientTrusted(
                java.security.cert.X509Certificate[] certs, String authType)
                throws java.security.cert.CertificateException {
            return;
        }
    }

}
